﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Dal
{
    public class ObjectDao
    {
        public static DataTable getObjectsbyKind(string kind)
        {
            string connection = Credentials.getCredentias("DMSTools");
            if (connection == null)
            {
                return null;
            }
            string consulta = string.Empty;
            switch (kind)
            {
                case "TAB":
                    consulta = "select * from DMS_Objetos where ObjetoTipo='TAB' order by ObjetoIndice";
                    break;
                case "GROUP":
                    consulta = "select O2.Objetokey Tab,O1.ObjetoKey,O1.ObjetoDescripcion from DMS_Objetos O1," +
                    "DMS_Objetos o2 where O1.Objetotipo='GROUP' and O1.ObjetoContenedor=O2.ObjetoId order by O1.ObjetoIndice";
                    break;
                case "TOOL":
                    consulta = "select O3.ObjetoKey Tab,O2.Objetokey grupo,O1.ObjetoKey,O1.ObjetoDescripcion,O1.Objetoimagen,O1.ObjetoToolType " +
                    "from DMS_Objetos O1,DMS_Objetos o2,DMS_Objetos o3 " +
                    "where O1.Objetotipo='TOOL' and O1.ObjetoContenedor=O2.ObjetoId and O2.ObjetoContenedor=O3.ObjetoID order by O1.ObjetoIndice";
                    break;
                case "Environment":
                    DataTable Environments = new DataTable("environment");
                    Environments.Columns.Add("name", typeof(string));
                    Environments.Columns.Add("type", typeof(string));
                    Environments.Columns.Add("project", typeof(string));
                    Credentials.XmlToDataSet("Credentials.xml", ref Environments);
                    return Environments;
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            cmd.CommandText = consulta;
            cmd.CommandType = System.Data.CommandType.Text;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dtObject");

            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                //Nombre del método que produjo la excepción.
                Credentials.writeLogFile("getObjectsbyKind(" + kind + "): " + ex.Message);
                return null;
            }
            return dt;
        }

        public static DataTable getMenu(string rol) {
            string connection = Credentials.getCredentias("DMSTools");
            if (connection == null)
            {
                return null;
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "GetMenu";
            cmd.Parameters.AddWithValue("@rol", rol);
            cmd.CommandTimeout = 0;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dtMenu");

            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getMenu: " + ex.Message);
                return null;
            }
            /*DataSet ds = new DataSet("dsMenu");
            ds.Tables.Add(dt);*/
            //return ds;
            return dt;
        }
    }
}